<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="5">
            <el-form-item label="标准物质名称：" prop="bzwzName" label-width="">
              <el-input clearable maxlength="100" placeholder="请输入标准物质名称" v-model="queryParams.bzwzName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="种类：" prop="bzwzType" label-width="80">
              <el-select placeholder="请选择" clearable v-model="queryParams.bzwzType" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="唯一性编号：" prop="bzwzCode" label-width="">
              <el-input clearable maxlength="100" placeholder="请输入唯一性编号" @keyup.enter="handleQuery"
                v-model="queryParams.bzwzCode" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="批次号：" prop="produceCode" label-width="65">
              <el-input clearable maxlength="100" placeholder="请输入批次号" @keyup.enter="handleQuery"
                v-model="queryParams.produceCode" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="检测项目：" prop="bucketId" label-width="80">
              <el-select v-model="queryParams.bucketId" clearable filterable remote reserve-keyword remote-show-suffix
                :remote-method="remoteMethod" :loading="loading" style="width: 100%">
                <el-option v-for="item in jcxmList" :key="item.bucketId" :value="item.bucketId" :label="item.jcxmName">
                  <span v-html="transformation(item.jcxmName)"></span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="检测项目类型：" prop="isSingle" label-width="">
              <el-select placeholder="请选择" clearable v-model="queryParams.isSingle" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_jcxm_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="归属类别：" prop="gsType" label-width="80">
              <el-select placeholder="请选择" clearable v-model="queryParams.gsType" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_gslb)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">
            <el-form-item label="领用日期：" prop="expirationType">
              <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" :clearable="false"
                :editable="false" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="生产厂商：" prop="produceName" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入生产厂商" @keyup.enter="handleQuery"
                v-model="queryParams.produceName" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="领用人：" prop="createUserId" label-width="65">
              <el-select clearable v-model="queryParams.createUserId" style="width: 100%" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(userList, 'userId', 'nickName')" :key="dict.userId"
                  :label="dict.nickName" :value="dict.userId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="保存位置：" prop="saveLocation" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入保存位置" @keyup.enter="handleQuery"
                v-model="queryParams.saveLocation" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="状态：" prop="">
              <el-select clearable style="width: 100%" filterable>
                <el-option />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-row justify="center">
              <el-radio label="0" v-model="queryParams.isLike">模糊搜索</el-radio>
              <el-radio label="1" v-model="queryParams.isLike">精准搜索</el-radio>
            </el-row>
          </el-col>
          <el-col :span="2">
            <el-button type="success" @click="handleQuery">搜索</el-button>
            <el-button class="resetBtn" @click="resetQuery">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation">领用标准物质列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <el-row style="padding-bottom: 5px" justify="end">
          <el-button link icon="FolderChecked" type="success" @click="exportDateFormPdf"
            >导出领用记录</el-button
          >
        </el-row>
        <el-table ref="mainTableRef" :data="bzwzTable" style="margin-top: 5px" @selection-change="handleSelChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="唯一性编号" prop="bzwz.bzwzCode" align="center" width="140" />
          <el-table-column label="标准物质名称" prop="bzwz.bzwzName" align="center" width="230" />
          <el-table-column label="种类" prop="bzwz.bzwzType" align="center" width="70">
            <template #default="scope">
              <dict-tag :options="sys_bzwz_type" :value="scope.row.bzwz.bzwzType" />
            </template>
          </el-table-column>
          <el-table-column label="生产厂商" prop="bzwz.produceName" align="center" />
          <el-table-column label="有效日期" prop="bzwz.yxDate" align="center" width="100" />
          <el-table-column label="批次号" prop="bzwz.produceCode" align="center" width="100" />
          <el-table-column label="规格" align="center" width="80">
            <template #default="scope">
              {{ scope.row.bzwz.totalGg }}
              <span v-html="transformation(scope.row.bzwz.ggUnitName)
                "></span>
            </template>
          </el-table-column>
          <el-table-column label="领用量" prop="outGg" align="center" width="70" />
          <el-table-column label="检测项目数" prop="bzwz.jcxmCount" align="center" width="100">
            <template #default="scope">
              <el-button link @click="jcxmCountClick(scope.row.bzwz.bzwzId)">{{
                scope.row.bzwz.jcxmName ? scope.row.bzwz.jcxmName : scope.row.bzwz.jcxmCount
              }}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="领用人" prop="createUserId" align="center" width="80">
            <template #default="scope">
              {{
                proxy.getItemByArray(
                  userList,
                  "userId",
                  scope.row.createUserId,
                  "nickName"
                )
              }}
            </template>
          </el-table-column>
          <el-table-column label="领用时间" prop="outDate" align="center" width="100" />
          <el-table-column label="发放人" prop="" align="center" width="80" />
          <el-table-column label="状态" prop="" align="center" width="80" />
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <!-- 点击行内检测项目数的弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="jcxmOpen" @close="closedDialog" :title="title">
      <el-row>
        <p class="pinformation">检测项目列表</p>
        <p class="totalStyle">共 {{ multiplexTable.length }} 条数据</p>
      </el-row>
      <el-table :data="multiplexTable">
        <el-table-column label="检测项目" prop="bucketId" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.jcxmName)
              "></span>
          </template>
        </el-table-column>
        <el-table-column label="浓度值" prop="con" align="center" />
        <el-table-column label="浓度值单位" prop="conUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.conUnitName)
              "></span>
          </template>
        </el-table-column>
        <el-table-column label="不确定度值" prop="notvalue" align="center" />
        <el-table-column label="不确定度单位" prop="notvalueUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.subNotvalueName)
              "></span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="jcxmOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BzwzRecord">
import { listUseList, bzwzList, getJcxmList } from "@/api/resource/ledger";
import { getUnitList } from "@/api/store/unit";
import { listUser } from "@/api/contract/check";
const { proxy } = getCurrentInstance();
const { sys_bzwz_type, sys_bzwz_gslb, sys_bzwz_gqlx, sys_bzwz_jcxm_type } =
  proxy.useDict(
    "sys_bzwz_type",
    "sys_bzwz_gslb",
    "sys_bzwz_gqlx",
    "sys_bzwz_jcxm_type"
  );

const bzwzTable = ref([]);
const multiplexTable = ref([]);
const jcxmList = ref([]);
const title = ref("查看检测项目");
const jcxmOpen = ref(false);
const data = reactive({
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    isLike: "0",
    bzwzType: -1,
    isSingle: -1,
    gsType: -1
  },
});

const { queryParams } = toRefs(data);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

const total = ref(0);
const dateRange = ref([]);
function getList() {
  let temp = {
    'bzwz.bzwzName': queryParams.value.bzwzName,
    'bzwz.bzwzCode': queryParams.value.bzwzCode,
    'bzwz.bzwzType': queryParams.value.bzwzType,
    'bzwz.produceCode': queryParams.value.produceCode,
    'bzwz.bucketId': queryParams.value.bucketId,
    'bzwz.isSingle': queryParams.value.isSingle,
    'bzwz.gsType': queryParams.value.gsType,
    'bzwz.produceName': queryParams.value.produceName,
    'bzwz.saveLocation': queryParams.value.saveLocation,
    'bzwz.isLike': queryParams.value.isLike,
    createUserId: queryParams.value.createUserId,
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize
  }
  listUseList(proxy.addDateRange(temp, dateRange.value)).then((res) => {
    bzwzTable.value = res.rows;
    total.value = res.total;
  });
}
getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  dateRange.value = []
  proxy.resetForm("queryRef");
  queryParams.value.isLike = "0"
  handleQuery();
}

function jcxmCountClick(row) {
  bzwzList({ bzwzId: row }).then((res) => {
    multiplexTable.value = res.data;
    jcxmOpen.value = true;
  });
}

function closedDialog() {
  multiplexTable.value = [];
}

const unitList = ref([]);
getUnitList({ unitGroup: 0 }).then((res) => {
  unitList.value = res.data;
});

function handleSelChange(selection) {
  multiplexTable.value = selection;
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}


const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});

const loading = ref(false);

function remoteMethod(val) {
  if (val) {
    loading.value = true;
    setTimeout(() => {
      getJcxmList({ jcxmName: val }).then((res) => {
        loading.value = false;
        jcxmList.value = res.rows;
      });
    }, 200);
  } else {
    jcxmList.value = [];
  }
}

function exportDateFormPdf() {
  if(dateRange.value.length == 0){
    proxy.$modal.msgError("请选择领用时间！");
    return;
  }
  proxy.addDateRange(queryParams.value, dateRange.value)
  if(queryParams.value.params){
    Object.assign(queryParams.value, queryParams.value.params)
    delete queryParams.value.params;
  }
  const params = {modelType: 18,params:queryParams.value};
  proxy.download("/flowable/task/exportReportPdf", params, `标准物质领用记录.pdf`);
}
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
